package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityDbBinding;
import com.hjqjl.whdemo.db.room.entity.Anime;
import com.hjqjl.whdemo.db.room.database.AppDatabase;
import com.hjqjl.whdemo.db.room.dao.AnimeDao;
import com.hjqjl.whdemo.db.sqlite.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DBActivity extends BaseActivity {
    private AnimeDao animeDao;

    public static void start(Context context) {
        Intent starter = new Intent(context, DBActivity.class);
        context.startActivity(starter);
    }

    ActivityDbBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDbBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        animeDao = AppDatabase.getInstance().animeDao();
        PermissionUtils.permission(PermissionConstants.STORAGE).request();//6.0以上必须有动态申请权限，应该在首页申请权限，不然不让用。
        binding.btnSqlite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DAO sqlHelper = new DAO(pActivity);
                List<String> tempList = new ArrayList<String>();
                String deleteSQL = "INSERT INTO Anime VALUES(NULL,'青春猪头少年不会梦到兔女郎学姐','奇幻','2019','12')";
                tempList.add(deleteSQL);
                sqlHelper.Transaction(tempList);
            }
        });
        binding.btnDbInsert.setOnClickListener(v -> {
            //增加一条动漫记录
            new Thread(new Runnable() {
                @Override
                public void run() {
                    animeDao.insertOneAnime(createOneAnime());
                }
            }).start();

        });

        binding.btnDbSelect.setOnClickListener(v -> {

            final StringBuilder builder = new StringBuilder();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    List<Anime> animeList = animeDao.getAllAnime();
                    for (Anime anime : animeList) {
                        builder.append(anime.getName());
                        LogUtils.v(anime.getName() + "---" + animeList.size());
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            binding.tvShow.setText(builder.toString());
                        }
                    });
                }
            }).start();


        });
    }

    private Anime createOneAnime() {
        Anime anime = new Anime();
        anime.setName("青春猪头少年不会梦到兔女郎学姐" + new Random().nextInt(100));
        anime.setType("奇幻&恋爱");
        anime.setPlayDate("2018-10");
        anime.setEpisode(13);
        return anime;
    }
}